# _*_ coding: utf-8 _*_

from __future__ import unicode_literals
from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
from mptt.models import MPTTModel, TreeForeignKey
import uuid
from system.models import Region, Company, UserProfile
from DjangoUeditor.models import UEditorField


# Create your models here.

class Navigation(MPTTModel):
    """栏目"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    parent = TreeForeignKey('self', related_name="children", verbose_name=u"父栏目", null=True, blank=True)
    name = models.CharField(max_length=64, verbose_name=u"栏目名称", default="", unique=True)

    review_count = models.IntegerField(null=True, blank=True, default=0, verbose_name=u"总浏览量")
    isShow = models.BooleanField(choices=((True, u"显示"), (False, u"隐藏")), default=True, verbose_name=u"是否显示")
    avator = models.ImageField(upload_to="image/navigation/%Y/%m", default=u"image/navigation/default.png", max_length=100, verbose_name=u"栏目图片", null=True, blank=True)

    url = models.URLField(verbose_name=u"栏目地址", default="", blank=True)
    company = models.ForeignKey(Company, verbose_name=u"所属公司", null=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"栏目"
        verbose_name_plural = verbose_name
        db_table = "tb_navigation"

    def __unicode__(self):
        return self.name


class BaseContent(models.Model):
    """内容管理"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    nav = models.ForeignKey(Navigation, verbose_name=u"所属栏目", null=True)
    
    name = models.CharField(max_length=127, verbose_name=u"名字", default="", unique=True)
    title = models.CharField(max_length=127, verbose_name=u"网页标题", default="", blank=True)
    des = models.CharField(max_length=127, verbose_name=u"网页描述", default="", blank=True)
    keywords = models.CharField(max_length=255, verbose_name=u"网页关键字", default="", blank=True)
    avator = models.ImageField(upload_to="image/content/%Y/%m", default=u"image/content/default.png", max_length=100, verbose_name=u"图片", null=True, blank=True)

    click_count = models.IntegerField(null=True, blank=True, default=0, verbose_name=u"点击量")

    isEnabled = models.BooleanField(choices=((True, u"启用"), (False, u"禁用")), default=True, verbose_name=u"是否启用")

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.name

class HouseType(models.Model):
    """房屋户型"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=64, verbose_name=u"户型名字", default="", unique=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"房屋户型"
        verbose_name_plural = verbose_name
        db_table = "tb_housetype"

    def __unicode__(self):
        return self.name

class HouseBuilding(BaseContent):
    """楼盘"""
    region = models.ForeignKey(Region, verbose_name=u"所在区域", null=True, blank=True)
    addr = models.CharField(max_length=255, verbose_name=u"详细地址", default="")
    description = models.CharField(max_length=1024, verbose_name=u"楼盘描述", default="", blank=True)
    sign_count = models.IntegerField(null=True, blank=True, default=0, verbose_name=u"签约数量")
    build_count = models.IntegerField(null=True, blank=True, default=0, verbose_name=u"在建数量")

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"楼盘图片"

    class Meta:
        verbose_name = u"楼盘"
        verbose_name_plural = verbose_name
        db_table = "tb_house"

class HouseBuildingImage(models.Model):
    """楼盘描述图片"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    house = models.ForeignKey(HouseBuilding, verbose_name=u"楼盘", related_name="images", null=True, blank=True)

    title = models.CharField(max_length=64, verbose_name=u"图片标题", default="", blank=True)
    desc = models.CharField(max_length=255, verbose_name=u"图片描述", default="", blank=True)
    avator = models.ImageField(upload_to="image/house/%Y/%m", default=u"image/house/default.png", max_length=100, verbose_name=u"楼盘描述图片")

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"楼盘描述图片"
        verbose_name_plural = verbose_name
        db_table = "tb_houseimage"

    def __unicode__(self):
        return self.title

class DesignerJob(models.Model):
    """设计师岗位"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=64, verbose_name=u"设计师岗位名字", default="", unique=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"设计师岗位"
        verbose_name_plural = verbose_name
        db_table = "tb_designerjob"

    def __unicode__(self):
        return self.title

class Designer(BaseContent):
    """设计师"""
    name = models.CharField(max_length=127, verbose_name=u"姓名", default="", unique=True)
    avator = models.ImageField(upload_to="image/designer/%Y/%m", default=u"image/designer/default.png", max_length=100, verbose_name=u"设计师头像", null=True)
    mobile = models.CharField(max_length=64, verbose_name=u"设计师电话", default="", blank=True)
    job = models.ForeignKey(DesignerJob, verbose_name=u"设计师岗位", null=True, blank=True)
    years = models.IntegerField(null=True, blank=True, default=0, verbose_name=u"从业年数")
    word = models.CharField(max_length=255, verbose_name=u"自己的名言", default="", blank=True)
    description = models.CharField(max_length=1024, verbose_name=u"人物介绍", default="", blank=True)

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"设计师头像"

    def get_goodstyles(self):
        #获取擅长风格
        styles = ''
        for goodstyle in self.goodstyles.all():
            styles += goodstyle.style.name + '，'
        return styles
    get_goodstyles.short_description = u"擅长风格"

    class Meta:
        verbose_name = u"设计师"
        verbose_name_plural = verbose_name
        db_table = "tb_designer"




class HouseStyle(models.Model):
    """房屋风格"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=64, verbose_name=u"风格名字", default="", unique=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"风格管理"
        verbose_name_plural = verbose_name
        db_table = "tb_housestyle"

    def __unicode__(self):
        return self.name



class DesignerGoodStyle(models.Model):
    """设计师擅长风格"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    designer = models.ForeignKey(Designer, verbose_name=u"设计师", related_name="goodstyles", null=True)
    style = models.ForeignKey(HouseStyle, verbose_name=u"擅长风格", null=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"设计师擅长风格"
        verbose_name_plural = verbose_name
        db_table = "tb_deisgnergoodstyle"

    def __unicode__(self):
        return '{0}({1})'.format(self.designer, self.style)

class Case(BaseContent):
    """装修案例"""
    avator = models.ImageField(upload_to="image/case/%Y/%m", default=u"image/case/default.png", max_length=100, verbose_name=u"标志图片", null=True)
    house = models.ForeignKey(HouseBuilding, verbose_name=u"楼盘", null=True, blank=True)
    area = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=0, verbose_name=u"面积")
    housetype = models.ForeignKey(HouseType, verbose_name=u"户型", null=True, blank=True)
    housestyle = models.ForeignKey(HouseStyle, verbose_name=u"风格", null=True, blank=True)
    designer = models.ForeignKey(Designer, verbose_name=u"设计师", related_name='cases', null=True, blank=True)
    idea_desc = models.CharField(max_length=512, verbose_name=u"设计理念描述", default="")

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"标志图片"

    class Meta:
        verbose_name = u"装修案例"
        verbose_name_plural = verbose_name
        db_table = "tb_cases"

class CaseImage(models.Model):
    """案例描述图片"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    case = models.ForeignKey(Case, verbose_name=u"案例", related_name="images", null=True, blank=True)

    title = models.CharField(max_length=64, verbose_name=u"图片标题", default="", blank=True)
    desc = models.CharField(max_length=255, verbose_name=u"图片描述", default="", blank=True)
    avator = models.ImageField(upload_to="image/caseDesc/%Y/%m", default=u"image/caseDesc/default.png", max_length=100, verbose_name=u"案例描述图片")

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"案例描述图片"
        verbose_name_plural = verbose_name
        db_table = "tb_caseimage"

    def __unicode__(self):
        return self.title


class Banner(BaseContent):
    """首页banner"""
    avator = models.ImageField(upload_to="image/banner/%Y/%m", default=u"image/banner/default.png", max_length=100, verbose_name=u"banner图片")

    rich_desc = UEditorField(verbose_name = u"富文本描述", width=600, height=300, imagePath="static/media/banner/%(basename)s_%(datetime)s.%(extname)s", filePath="static/media/banner/%(basename)s_%(datetime)s.%(extname)s", default='', blank=True)

    isShow = models.BooleanField(choices=((True, u"显示"), (False, u"隐藏")), default=True, verbose_name=u"是否显示")

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"banner图片"

    def show_rich_desc(self):
        #显示富文本
        from django.utils.safestring import mark_safe
        return mark_safe(self.rich_desc)
    show_rich_desc.short_description = u"富文本描述"

    class Meta:
        verbose_name = u"首页banner"
        verbose_name_plural = verbose_name
        db_table = "tb_banner"

class Article(BaseContent):
    """新文章"""
    description = models.CharField(max_length=512, verbose_name=u"文章描述", default="")
    rich_content = UEditorField(verbose_name = u"富文本内容", width=600, height=300, imagePath="static/media/article/%(basename)s_%(datetime)s.%(extname)s", filePath="static/media/article/%(basename)s_%(datetime)s.%(extname)s", default='')
    writor = models.ForeignKey(UserProfile, verbose_name=u"作者", related_name="articles", null=True, blank=True)
    issubmit = models.BooleanField(choices=((True, u"是"), (False, u"否")), default=False, verbose_name=u"是否提交审核")
    reviewer = models.ForeignKey(UserProfile, verbose_name=u"审核者", related_name="review_articles", null=True, blank=True)
    review_time = models.DateTimeField(null=True, blank=True, verbose_name=u"审核时间")
    review_status = models.IntegerField(choices=((0, u"未审核"), (1, u"审核通过"), (-1, u"审核不通过")), default=0, verbose_name=u"审核结果")

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"文章图片"

    class Meta:
        verbose_name = u"新文章"
        verbose_name_plural = verbose_name
        db_table = "tb_article"

class ReviewArticle(Article):
    """审核文章"""
    class Meta:
        verbose_name = "审核文章"
        verbose_name_plural = verbose_name
        proxy = True

class ReviewedArticle(Article):
    """已审核文章"""
    class Meta:
        verbose_name = "已审核文章"
        verbose_name_plural = verbose_name
        proxy = True


class GlobalActivity(BaseContent):
    """全局活动"""
    rich_content = UEditorField(verbose_name = u"富文本内容", width=600, height=300, imagePath="static/media/globalactivity/%(basename)s_%(datetime)s.%(extname)s", filePath="static/media/globalactivity/%(basename)s_%(datetime)s.%(extname)s", default='')

    description = models.CharField(max_length=1024, verbose_name=u"活动描述", default="")

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"全局活动图片"

    class Meta:
        verbose_name = u"全局活动"
        verbose_name_plural = verbose_name
        db_table = "tb_globalactivity"


class FurnitureType(models.Model):
    """家具类型"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=64, verbose_name=u"类型名称", default="", unique=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"家具类型"
        verbose_name_plural = verbose_name
        db_table = "tb_furnituretype"

    def __unicode__(self):
        return self.name

class FurnitureBrand(models.Model):
    """家具品牌"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=64, verbose_name=u"品牌名称", default="", unique=True)

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"家具品牌"
        verbose_name_plural = verbose_name
        db_table = "tb_furniturebrand"

    def __unicode__(self):
        return self.name

class Furniture(BaseContent):
    """家具"""
    ftype = models.ForeignKey(FurnitureType, verbose_name=u"家具类型", null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True, default=0, verbose_name=u"价格")
    brand = models.ForeignKey(FurnitureBrand, verbose_name=u"品牌", related_name="furnitures", null=True, blank=True)
    series = models.CharField(max_length=64, verbose_name=u"系列", default="", blank=True)
    model = models.CharField(max_length=128, verbose_name=u"型号", default="", blank=True)
    spec = models.CharField(max_length=128, verbose_name=u"规格", default="", blank=True)
    style = models.CharField(max_length=128, verbose_name=u"风格", default="", blank=True)
    caizhi = models.CharField(max_length=128, verbose_name=u"材质", default="", blank=True)

    def show_avator(self):
        #显示图片
        from django.utils.safestring import mark_safe
        return mark_safe("<img src='" + self.avator.url + "' width='100' height='100'></img>")
    show_avator.short_description = u"家具图片"

    class Meta:
        verbose_name = u"家具"
        verbose_name_plural = verbose_name
        db_table = "tb_furniture"


class FurnitureImage(models.Model):
    """家具描述图片"""
    id = models.UUIDField(primary_key=True, auto_created=True, default=uuid.uuid4, editable=False)
    furniture = models.ForeignKey(Furniture, verbose_name=u"家具", null=True, related_name='images', blank=True)

    title = models.CharField(max_length=64, verbose_name=u"图片标题", default="", blank=True)
    desc = models.CharField(max_length=255, verbose_name=u"图片描述", default="", blank=True)
    avator = models.ImageField(upload_to="image/caseDesc/%Y/%m", default=u"image/caseDesc/default.png", max_length=100, verbose_name=u"描述图片")

    updated_at = models.DateTimeField(auto_now=True, verbose_name=u"更新时间")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=u"创建时间")

    class Meta:
        verbose_name = u"家具描述图片"
        verbose_name_plural = verbose_name
        db_table = "tb_furnitureimage"

    def __unicode__(self):
        return self.title



    

